﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class ForkLoaderController : MonoBehaviour
{
    public Transform liftObj;
    public float movespeed = 4f;
    public float carspeed = 2.5f;
    public float wheelspeed = 300f;
    public float rotation = 100f;
    public Transform car;//车辆本体
    public Transform fl;//左前轮
    public Transform fr;//右前轮
    public Transform rl;//左后轮
    public Transform rr;//右后轮
    void Start()
    {
        if (car == null)
        {
            Debug.LogError("找不到已绑定的车辆本体,尝试自动加载......");
            car = transform.Find("car");
        }
        if (fl == null)
        {
            Debug.LogError("找不到已绑定的左前轮,尝试自动加载......");
            fl = transform.Find("body/wheelL1");
        }
        if (fr == null)
        {
            Debug.LogError("找不到已绑定的右前轮,尝试自动加载......");
            fr = transform.Find("body/wheelR1");
        }
        if (rl == null)
        {
            Debug.LogError("找不到已绑定的左后轮,尝试自动加载......");
            rl = transform.Find("body/wheelL2");
        }
        if (rr == null)
        {
            Debug.LogError("找不到已绑定的右后轮,尝试自动加载......");
            rr = transform.Find("body/wheelR2");
        }
        if (liftObj == null)
        {
            Debug.LogError("找不到已绑定的叉子物体,尝试自动加载......");
            liftObj = transform.Find("lift");
        }
    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.tag.Equals("MoveBox"))
            other.transform.parent = liftObj;
        if (GetComponent<BoxCollider>().isTrigger)
            GetComponent<BoxCollider>().enabled = false;
    }


    void Update()
    {
        //if (liftObj.localPosition.y<=2.0f)
        if (Input.GetKey("q") && liftObj.localPosition.y <= 2.0f)
        {
            liftObj.Translate(Vector3.up * Time.deltaTime * movespeed);
        }
        if (Input.GetKey("z") && liftObj.localPosition.y >= 0.75f)
        {
            liftObj.Translate(Vector3.down * Time.deltaTime * movespeed);
        }
        if (Input.GetKey("w"))
        {
            car.Translate(Vector3.forward * Time.deltaTime * carspeed);
            fl.transform.Rotate(Vector3.right * Time.deltaTime * wheelspeed);       
            fr.transform.Rotate(Vector3.right * Time.deltaTime * wheelspeed);
            rl.transform.Rotate(Vector3.right * Time.deltaTime * wheelspeed);
            rr.transform.Rotate(Vector3.right * Time.deltaTime * wheelspeed);
        }
        if (Input.GetKey("s"))
        {
            car.Translate(Vector3.back * Time.deltaTime * carspeed);
            fl.transform.Rotate(Vector3.left * Time.deltaTime * wheelspeed);
            fr.transform.Rotate(Vector3.left * Time.deltaTime * wheelspeed);
            rl.transform.Rotate(Vector3.left * Time.deltaTime * wheelspeed);
            rr.transform.Rotate(Vector3.left * Time.deltaTime * wheelspeed);
        }
        
        if (Input.GetKey("a"))
        {
            car.Rotate(Vector3.down * Time.deltaTime * rotation);
            fl.transform.localEulerAngles += new Vector3(0, -100f * Time.deltaTime, 0);
            rl.transform.localEulerAngles += new Vector3(0, -100f * Time.deltaTime, 0);
        }
        if (Input.GetKey("d"))
        {
            car.Rotate(Vector3.up * Time.deltaTime * rotation);
            fl.transform.Rotate(Vector3.up * Time.deltaTime * wheelspeed);
            rl.transform.Rotate(Vector3.up * Time.deltaTime * wheelspeed);
        }
        
        float translation = Input.GetAxis("Vertical") * movespeed * Time.deltaTime;
        car.Translate(Vector3.forward * translation);
        float rotation1 = Input.GetAxis("Horizontal") * 100f * Time.deltaTime;
        car.Rotate(Vector3.up * Time.deltaTime * rotation1);
    }
}